//
//  ReSetPwdViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/13.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "ReSetPwdViewController.h"

@interface ReSetPwdViewController ()
@property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
@property (weak, nonatomic) IBOutlet UIView *FirstLabel;
@property (weak, nonatomic) IBOutlet UIView *SecondLabel;
@property (weak, nonatomic) IBOutlet UIView *ThirdLabel;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *PasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *RePasswordTF;
@property (weak, nonatomic) IBOutlet UIButton *SubmitBtn;

@end

@implementation ReSetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _PhoneTF.text = self.phoneNum;
    _PhoneTF.keyboardType = UIKeyboardTypeNumberPad;
    [self createUI];
}

- (void)createUI{
    self.view.backgroundColor = [UIColor whiteColor];
    _TitleLabel.layer.borderColor = [[UIColor whiteColor]CGColor];
    _TitleLabel.layer.borderWidth = 1.0f;
    [_TitleLabel.layer setCornerRadius:15.0];
    _TitleLabel.layer.masksToBounds = YES;
    
    [_SubmitBtn.layer setCornerRadius:20.0];
    _SubmitBtn.layer.masksToBounds = YES;
    
    [self createLabel:_FirstLabel];
    [self createLabel:_SecondLabel];
    [self createLabel:_ThirdLabel];
}

- (void)createLabel:(UIView *)label{
    label.layer.borderColor = [[UIColor colorWithHex:0xebebeb]CGColor];
    label.layer.borderWidth = 1.0f;
    [label.layer setCornerRadius:17.0];
    label.layer.masksToBounds = YES;
}

- (IBAction)BackCilck:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)SubmitClick:(UIButton *)sender {
    if (_PhoneTF.text.length == 0 || _PhoneTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text.length == 0 || _RePasswordTF.text.length == 0) {
        [self.view Message:@"请输入密码" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text.length < 6 || _RePasswordTF.text.length > 12) {
        [self.view Message:@"密码长度应在6-12位" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text != _RePasswordTF.text) {
        [self.view Message:@"两次密码不一样" HiddenAfterDelay:1];
        return;
    }
    [self.view loadingOnAnyView];
    NSData *PSD = [_PasswordTF.text
                   dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64PSD = [PSD base64EncodedStringWithOptions:0];
    NSDictionary *dic = @{@"userPhone":_PhoneTF.text,@"loginPwd":base64PSD,@"reloginPwd":base64PSD};
    [THWManager PostHttp:API(EditPSW) Parameters:dic Success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [self.view removeAnyView];
            if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                [self.view Message:responseObject[@"msg"]];
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
                
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
                });
            }else{
                [self.view Message:responseObject[@"msg"]];
            }
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_PhoneTF resignFirstResponder];
    [_PasswordTF resignFirstResponder];
    [_RePasswordTF resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
